package org.example.controller;

import org.example.entity.CustomerOrder;
import org.example.service.CustomerOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 客户订单控制器
 */
@RestController
@RequestMapping("/api/customer-order")
public class CustomerOrderController {
    
    @Autowired
    private CustomerOrderService customerOrderService;
    
    /**
     * 获取所有订单
     */
    @GetMapping("/list")
    public List<CustomerOrder> getAllOrders() {
        return customerOrderService.list();
    }
    
    /**
     * 根据ID获取订单
     */
    @GetMapping("/{id}")
    public CustomerOrder getOrderById(@PathVariable Long id) {
        return customerOrderService.getById(id);
    }
    
    /**
     * 根据订单号查询订单
     */
    @GetMapping("/search/order-no")
    public CustomerOrder getOrderByOrderNo(@RequestParam String orderNo) {
        return customerOrderService.getByOrderNo(orderNo);
    }
    
    /**
     * 根据客户姓名查询订单
     */
    @GetMapping("/search/name")
    public List<CustomerOrder> getOrdersByName(@RequestParam String plName) {
        return customerOrderService.getByPlName(plName);
    }
    
    /**
     * 根据客户手机号查询订单
     */
    @GetMapping("/search/phone")
    public List<CustomerOrder> getOrdersByPhone(@RequestParam String plPhone) {
        return customerOrderService.getByPlPhone(plPhone);
    }
    
    /**
     * 根据订单状态查询订单
     */
    @GetMapping("/search/status")
    public List<CustomerOrder> getOrdersByStatus(@RequestParam String status) {
        return customerOrderService.getByStatus(status);
    }
    
    /**
     * 根据放款金额范围查询订单
     */
    @GetMapping("/search/loan-amount")
    public List<CustomerOrder> getOrdersByLoanAmountRange(
            @RequestParam(required = false) BigDecimal minAmount,
            @RequestParam(required = false) BigDecimal maxAmount) {
        return customerOrderService.getByLoanAmountRange(minAmount, maxAmount);
    }
    
    /**
     * 根据放款日期范围查询订单
     */
    @GetMapping("/search/loan-date")
    public List<CustomerOrder> getOrdersByLoanDateRange(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        LocalDate start = startDate != null ? LocalDate.parse(startDate) : null;
        LocalDate end = endDate != null ? LocalDate.parse(endDate) : null;
        return customerOrderService.getByLoanDateRange(start, end);
    }
    
    /**
     * 根据城市查询订单
     */
    @GetMapping("/search/city")
    public List<CustomerOrder> getOrdersByCity(@RequestParam String cityName) {
        return customerOrderService.getByCity(cityName);
    }
    
    /**
     * 统计总放款金额
     */
    @GetMapping("/statistics/total-loan")
    public BigDecimal getTotalLoanAmount() {
        return customerOrderService.getTotalLoanAmount();
    }
    
    /**
     * 统计在途余额
     */
    @GetMapping("/statistics/outstanding")
    public BigDecimal getOutstandingAmount() {
        return customerOrderService.getOutstandingAmount();
    }
    
    /**
     * 分页查询订单
     */
    @GetMapping("/page")
    public List<CustomerOrder> getOrdersPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return customerOrderService.list();
    }
    
    /**
     * 创建订单
     */
    @PostMapping
    public boolean createOrder(@RequestBody CustomerOrder order) {
        return customerOrderService.save(order);
    }
    
    /**
     * 更新订单
     */
    @PutMapping
    public boolean updateOrder(@RequestBody CustomerOrder order) {
        return customerOrderService.updateById(order);
    }
    
    /**
     * 删除订单
     */
    @DeleteMapping("/{id}")
    public boolean deleteOrder(@PathVariable Long id) {
        return customerOrderService.removeById(id);
    }
}
